package com.kpx.app.wigdet;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.kpx.app.R;
import com.kpx.app.adapter.MenuAdapter;
import com.kpx.app.bean.MenuBean;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/10/14.
 */
public class PopShowTaskMenu extends PopupWindow {
    private ArrayList<MenuBean> beans;
    private View contentView;
    private GridView lvResults;

    //下拉列表项被单击的监听器
    private OnSelectClicListener listener;
    private final MenuAdapter adapter;

    public void setData(ArrayList<MenuBean> beans, int count) {
        if (adapter != null) {
            this.beans = beans;
            adapter.setData(beans);
            lvResults.setNumColumns(count);
        }
    }

    public interface OnSelectClicListener {
        void onClick(MenuBean bean);
    }

    public PopShowTaskMenu(final Context context,final ArrayList<MenuBean> list, int width, OnSelectClicListener itemClickListener) {
        super(context);
        this.listener = itemClickListener;
        contentView = View.inflate(context, R.layout.own_dropdown_glist, null);
        setContentView(contentView);
        lvResults = (GridView) contentView.findViewById(R.id.lvResults);
        View view_bottom = (View) contentView.findViewById(R.id.view_bottom);
//        View view_up = (View) contentView.findViewById(R.id.view_up);
        beans = list;
        adapter = new MenuAdapter(context, list, R.layout.item_pop_show_menu);
        lvResults.setAdapter(adapter);
//        view_bottom.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                dismiss();
//            }
//        });
//        view_up.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                dismiss();
//            }
//        });
        //如果PopupWindow中的、下拉列表项被单击了

        //则通知外部的下拉列表项单击监听器并传递当前单击项的数据
        lvResults.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View view, int index, long arg3) {
                if (listener != null) {
                    if (listener != null)
                        for (int i = 0; i < beans.size(); i++) {
                            beans.get(i).setSelected(false);
                        }
                    beans.get(index).setSelected(true);
                    adapter.notifyDataSetChanged();
                    listener.onClick(beans.get(index));
                    dismiss();
                }
            }
        });

        this.setContentView(contentView);  //设置悬浮窗体内显示的内容View
        this.setWidth(width - 5);   //设置悬浮窗体的宽度
        //this.setWidth(LayoutParams.WRAP_CONTENT);
        this.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);   //设置悬浮窗体的高度
        this.setBackgroundDrawable(new ColorDrawable(0x00000000)); // 设置悬浮窗体背景
//        this.setAnimationStyle(R.style.PopupAnimation);
        this.setAnimationStyle(R.anim.popup_enter);     //设置悬浮窗体出现和退出时的动画
        this.setFocusable(true);    // menu菜单获得焦点 如果没有获得焦点menu菜单中的控件事件无法响应
        this.setOutsideTouchable(true);   //可以再外部点击隐藏掉PopupWindow
    }

}
